package com.tianqu.tms.www.car.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.dto.VendorDto;
import com.tianqu.tms.www.car.controller.CarInformationShowController;
import com.tianqu.tms.www.car.viewhelper.CarInformationShowViewHelper;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class CarInformationShowAjaxAction extends TmsAjaxAction {
	private Logger logger = Logger.getLogger(CarInformationShowAjaxAction.class);
	private String SESSION_LIMIT = "limit";
	private int TITLE_NUM_OFFSET = 10;
	private String requestType = "1"; //init data

	@Override
	public void process(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, UserContext uc) {
		try {
			String type = request.getParameter("type");
			if(requestType.equals(type))  responseValues(response, getInitData(request,response,uc), TEXT);
			else responseValues(response, getTableData(request,response,uc), TEXT);
		} catch (Exception e) {
			logger.error(e);
		}
	}
	
	private String getTableData(HttpServletRequest request, HttpServletResponse response, UserContext uc) throws Exception{
		String vendorId = request.getParameter("vendor_id");
		logger.debug("CarInformationShowAjaxAction.getTableData,info,vendorId:"+vendorId);
		CarInformationShowController controller = new CarInformationShowController();
		controller.execute(uc,vendorId);
		//
		CarInformationShowViewHelper viewhelper = new CarInformationShowViewHelper();
		viewhelper = (CarInformationShowViewHelper) uc.getAttribute("tableData");
		JSONArray tableData = JSONArray.fromObject(viewhelper.getCarStyleCountDtoList());
		JSONObject json = new JSONObject();
		json.put("tableData", tableData);
		return json.toString();
	}
	
	private String getInitData(HttpServletRequest request, HttpServletResponse response, UserContext uc) throws Exception{
		logger.debug("CarInformationShowAjaxAction.getInitData,info");
		CarInformationShowController controller = new CarInformationShowController();
		controller.execute(uc);
		//
		CarInformationShowViewHelper viewhelper = new CarInformationShowViewHelper();
		viewhelper = (CarInformationShowViewHelper) uc.getAttribute("titles");
		List<VendorDto> list = viewhelper.getVendorDtolist();
		//
		String tmp = (String) request.getSession().getAttribute(SESSION_LIMIT);
		int limit = StringUtil.isNullOrBlank(tmp) ? 0 : Integer.parseInt(tmp); if(limit < 0) limit = 0;
        int offset = limit + TITLE_NUM_OFFSET; offset = offset > viewhelper.getTotal()?viewhelper.getTotal():offset;
		if(limit >= offset){  limit = 0; offset = TITLE_NUM_OFFSET; }
		request.getSession().setAttribute(SESSION_LIMIT, offset+1+"");
		
		//
		String date = DateUtil.getCurrentTimeStr();// 日期
		int carQuantity = 0;// 总车辆数
		
		ArrayList<HashMap<String,String>> titles = new ArrayList<HashMap<String,String>>();
		for (int i = 0; i < list.size(); i++) {
			VendorDto dto = list.get(i);
			carQuantity += dto.getOnlineCount();
			if(i >= limit && i<= offset){
				HashMap<String,String> titleMap = new HashMap<String,String>();
				titleMap.put("vendorId",dto.getVendorId());
				titleMap.put("enImage",dto.getEnImage());
				titleMap.put("vendorName",dto.getVendorName());
				titles.add(titleMap);
			}
		}
		
		JSONArray titleJsonArray = JSONArray.fromObject(titles);
		JSONObject json = new JSONObject();
		json.put("date", date);
		json.put("carQuantity", String.valueOf(carQuantity));
		json.put("carTitle", titleJsonArray);
		return json.toString();
	}
	 
}
